using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

/// <summary>
/// AKS 2.0 - HiSoftware Inc.
/// SA DESIGN:
/// Robert B. Yonaitis 
/// </summary>

/// <variables>
/// oOutput          -  An object used in outputting the finalized code
/// sOutputBuilder   -  A string containing the code to be rendered to the page
/// oWriter          -  An object used to output the rendered code
/// sControlID       -  A string used to store the id of the control being affected
/// sSearchString    -  A string used to store the string being used to locate the correct control to alter
/// iStart           -  An integer which stores the starting index of the sSearchString when located
/// iStepThroughName -  An integer used as an index to step through sOutputBuilder
/// </variables>

namespace AKSAdapters
{
	public class AKS_SearchBox_SmartAdapter : System.Web.UI.Adapters.ControlAdapter
	{
		public AKS_SearchBox_SmartAdapter()
		{

		}

		protected override void Render(HtmlTextWriter oOutput)
		{
			//Initializes sOutputBuilder as a new StringBuilder object
			StringBuilder sOutputBuilder = new StringBuilder();
			HtmlTextWriter oWriter = new HtmlTextWriter(new StringWriter(sOutputBuilder));
			base.Render(oWriter);

			//Initialize sControlID
			string sControlID = null;

			//Initialize sSearchString
			string sSearchString = null;

			//Assign sSearchString to the string used to identify the correct control
			sSearchString = "title=\"Search Scope\"";

			//Initialize iStart as the starting index of sSearchString
			int iStart = sOutputBuilder.ToString().IndexOf(sSearchString);

			//If iStart is greater than 0, sSearchString has been found and the adapter will proceed with the alteration(s)
			//Otherwise, the adapter will not change anything and proceeds to output.
			if (iStart > 0)
			{
				//Search for the character '<', stepping back one space through sOutputBuilder while not found
				while (sOutputBuilder[iStart] != '<')
				{
					iStart -= 1;
				}

				//Initialize iStepThroughName to the local variable iStart used to step through sOutputBuilder 
				//after sSearchString has been found, proceeding until the end of the control's name attribute is found.
				int iStepThroughName = iStart;

				//Add 14 to iStepThroughName to pass by '<select name="'
				iStepThroughName += 14;

				//Step through sOutputBuilder until it finds '"'
				while (sOutputBuilder[iStepThroughName] != '"')
				{
					//Assign the control's ID to sControlID
					sControlID += sOutputBuilder[iStepThroughName];
					iStepThroughName++;
				}

				//Replace all instances of $ with _
				sControlID = sControlID.Replace("$", "_");

				//Adds a label of "Search Scope" to the "Search Scope" text box
				sOutputBuilder.Insert(iStart, "<label for=\"" + sControlID + "\">Search Scope </label>");
			}

			//Clears the value of sControlID
			sControlID = "";

			//Assign sSearchString to the string used to identify the correct control
			sSearchString = "title=\"Enter search words\"";

			//Initialize iStart as the starting index of sSearchString
			iStart = sOutputBuilder.ToString().IndexOf(sSearchString);

			//If iStart is greater than 0, sSearchString has been found and the adapter will proceed with the alteration(s)
			//Otherwise, the adapter will not change anything and proceeds to output.
			if (iStart > 0)
			{
				//Search for the character '<', stepping back one space through sOutputBuilder while not found
				while (sOutputBuilder[iStart] != '<')
				{
					iStart -= 1;
				}

				//Initialize iStepThroughName to the local variable iStart used to step through sOutputBuilder 
				//after sSearchString has been found, proceeding until the end of the control's name attribute is found.
				int iStepThroughName = iStart;

				//Add 13 to iStepThroughName to pass by '<input name="'
				iStepThroughName += 13;

				//Step through sOutputBuilder until '"' is found
				while (sOutputBuilder[iStepThroughName] != '"')
				{
					//Assign the control's ID to sControlID
					sControlID += sOutputBuilder[iStepThroughName];
					iStepThroughName++;
				}

				//Replace all instances of $ with _
				sControlID = sControlID.Replace("$", "_");

				//Used to ensure that the label is applied to only this control
				if (sOutputBuilder[iStart - 29] == 't')
				{
					//Adds a label of "Enter Search Words" to the "Enter Search Words" text box
					sOutputBuilder.Insert(iStart, "<label for=\"" + sControlID + "\">Enter Search Words </label>");
				}
			}

			//Output sOutputBuilder to be rendered
			oOutput.Write(sOutputBuilder.ToString());
		}
	}
}